package com.example.forum2.controller;

import com.example.forum2.common.AppResult;
import com.example.forum2.common.ResultCode;
import com.example.forum2.congif.AppConfig;
import com.example.forum2.model.Message;
import com.example.forum2.model.User;
import com.example.forum2.services.IMessageServcie;
import com.example.forum2.services.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Api(tags="站内信接口")
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    private IMessageServcie iMessageServcie;
    @Resource
    private IUserService iUserService;
    @ApiOperation("回复信息")
    @PostMapping("/send")
    public AppResult message(HttpServletRequest request,
                             @ApiParam("接收者id")@RequestParam("receiveUserId") @NonNull Long receiveUserId,
                             @ApiParam("回复正文")@RequestParam("content")@NonNull String content){
//        获取当前登录用户的信息
        HttpSession session=request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
//        判断当前用户的信息,如果被禁言,则不能发送站内信
        if(user.getState()==1){
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
//        不能给自己发送站内信
        if(user.getId()==receiveUserId){
            log.warn("接受者不能是自己");
            return AppResult.failed(ResultCode.FAILED);
        }
//        校验接收者是否存在
        User user1=iUserService.selectById(receiveUserId);
        if(user1==null||user1.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
//        封装对象
        Message message=new Message();
        message.setReceiveUserId(receiveUserId);
        message.setPostUserId(user.getId());
        message.setContent(content);
//        调用servcie
        iMessageServcie.create(message);
        return AppResult.success();
    }


    @ApiOperation("获取未读数")
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request){
//        获取未读数
        HttpSession session=request.getSession(false);
        User user=(User)session.getAttribute(AppConfig.USER_SESSION);
//        调用servcie
        Integer count=iMessageServcie.selectUnreadCount(user.getId());
        return AppResult.success(count);
    }
//    根据登陆人id,显示所有的站内信
    @ApiOperation("查询用户的所有站内信")
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request){
//        获取session
        HttpSession session=request.getSession(false);
        User user=(User)session.getAttribute(AppConfig.USER_SESSION);
//        用户已经登录进入,是存在的
//        调用service
        List<Message> messages=iMessageServcie.selectByReceiveUserId(user.getId());
//返回结果
        return AppResult.success(messages);
    }
    @ApiOperation("更新为已读")
    @PostMapping("/markRead")
    public AppResult markRead(HttpServletRequest request,
            @ApiParam("站内信Id")@RequestParam("id")@NonNull Long id){
//        根据id查询站内信
        Message message=iMessageServcie.selectById(id);
//检查站内信是否存在
        if(message==null||message.getDeleteState()==1){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
//        站内信是不是自己的
        HttpSession session=request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getId()!=message.getReceiveUserId()){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
//        调用dao层,更新为已读
        iMessageServcie.updateStateById(id,(byte)1);
        return AppResult.success();
    }
//回复站内信
    @ApiOperation("回复站内信")
    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request,
                           @ApiParam("回复的站内信Id") @RequestParam("repliedId")@NonNull Long repliedId,
                           @ApiParam("回复的站内信内容")@RequestParam("content")@NonNull String content){
//        检验当前用户的状态
        HttpSession session=request.getSession(false);
        User user=(User)session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
//        检验要回复的站内信的状态
        Message existsMessage=iMessageServcie.selectById(repliedId);
        if(existsMessage==null||existsMessage.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
//        不能给自己回复
        if(user.getId()==existsMessage.getPostUserId()){
            return AppResult.failed("不能给自己回复");
        }
//        构造对象
        Message message=new Message();
        message.setContent(content);
        message.setPostUserId(user.getId());
        message.setReceiveUserId(existsMessage.getPostUserId());
//        调用servcie
        iMessageServcie.reply(repliedId,message);
//     返回结果
        return AppResult.success();
    }
}
